package com.gaoyi.basemodule.baseUtil;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import androidx.fragment.app.FragmentActivity;

public class WindowUtil {

    /**
     * 背景充满状态栏
     */
    public static void setWindowDecorView(Activity activity) {
        Window window = activity.getWindow();
        View decorView = window.getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        decorView.setFitsSystemWindows(true);
        window.setStatusBarColor(Color.TRANSPARENT);
    }

    /**
     * 背景充满状态栏
     */
    public static void hideWindowDecorView(Activity activity) {
        Window window = activity.getWindow();
        View decorView = window.getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        decorView.setFitsSystemWindows(true);
    }

    /**
     * 修改状态栏背景颜色
     *
     * @param activity
     * @param color
     */
    public static void setBarBgColor(Activity activity, int color) {
        activity.getWindow().setStatusBarColor(activity.getResources().getColor(color));
    }

    /**
     * 修改状态栏背景颜色为白色
     *
     * @param activity
     */
    public static void setWhiteBarBg(Activity activity) {
        activity.getWindow().setStatusBarColor(Color.WHITE);
        WindowUtil.setStatusBarTextColor(activity);
    }

    /**
     * 设置状态栏字体为黑色
     */
    public static void setStatusBarTextColor(Activity activity) {
        Window window = activity.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        View decorView = window.getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    }

    public static void hideSoftInput(View view) {
        InputMethodManager imm = (InputMethodManager) AppUtil.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    /**
     * 清除焦点
     */
    public static void clearFocus(FragmentActivity context) {
        View currentFocus = context.getCurrentFocus();
        if (currentFocus != null) {
            context.getCurrentFocus().clearFocus(); // 清除焦点，使EditInputView更新数据
        }
    }
}
